package com.tyjc.unique.dlt;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.tyjc.unique.bean.DLTTrendChart;
import com.tyjc.unique.common.DLTZouShiInfors;
import com.tyjc.unique.common.ZouShiInfors;
import com.tyjc.unique.net.StringResultBean;
import com.tyjc.unique.net.UrlConstants;
import com.tyjc.unique.ssq._AnalaysesBaseFragment;
import com.tyjc.unique.view.MoveScroll;
import com.tyjc.unique.view.MoveScrollAbstract;
import com.tyjc.unique.view.ZouShiOnDraw;
import com.fyzk.activity.zoushitu.ZST_sheZhi;
import com.tyjc.lottery.R;

import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

import http.Common.SchemaDef;

public abstract class ZouShiAnalaysesBaseFragment extends _AnalaysesBaseFragment implements
        android.widget.CompoundButton.OnCheckedChangeListener {


    public ArrayList<DLTTrendChart> arrayChartItems = null;
    protected ZouShiInfors infors;

    private TextView touTextView;
    private TextView diTextView;
    private LinearLayout sz_ll;

    private MoveScroll scr_top, scr_down, scr_left, scr_center;
    protected LinearLayout ll_center;
    private List<MoveScroll> scrs = new ArrayList<MoveScroll>();

    private int qiShu = 30;
    public boolean isZX = true, isYL = true, isTJ = true;
    public int type = 0;


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    @Override
    public void showData() {
        super.showData();
        requestTrendChart();
    }

    private void requestTrendChart() {

        RequestParams params = new RequestParams(UrlConstants.TEST);

        params.addBodyParameter("lotteryType", getLotteryType() + "");
        params.addBodyParameter("page", "1");
        params.addBodyParameter("count", "" + qiShu);

        httpPost(SchemaDef.TREND_CHART, params, false);

    }

    protected abstract int getLotteryType();


    public void initHistoryList() {

        change(DLTZouShiInfors.getZouShi(arrayChartItems, type));

        // setWH();
    }

    //
    // ======================================================================================================

    public void initView(View view) {

        sz_ll = (LinearLayout) view.findViewById(R.id.zst_shezhi_ll);
        sz_ll.setOnClickListener(this);

        touTextView = (TextView) view.findViewById(R.id.zst_center_tv_tou1);
        diTextView = (TextView)view. findViewById(R.id.zst_center_tv_di1);

        setWH();

        scr_top = (MoveScroll) view.findViewById(R.id.zst_center_scr_top1);
        scr_down = (MoveScroll) view.findViewById(R.id.zst_center_scr_bottom1);
        scr_left = (MoveScroll) view.findViewById(R.id.zst_center_scr_left1);
        scr_center = (MoveScroll)view. findViewById(R.id.zst_center_scr_center1);

        ll_center = new LinearLayout(getContext());

        scrs.add(scr_top);
        scrs.add(scr_center);
        scrs.add(scr_left);
        scrs.add(scr_down);

        scr_center.setType(MoveScrollAbstract.SCR_CENTER);
        scr_left.setType(MoveScrollAbstract.SCR_LEFT);
        scr_top.setType(MoveScrollAbstract.SCR_TOP);
        scr_down.setType(MoveScrollAbstract.SCR_BOTTOM);

        scr_top.setGuanLian(scrs);
        scr_down.setGuanLian(scrs);
        scr_left.setGuanLian(scrs);
        scr_center.setGuanLian(scrs);

        ll_center.addView(getCenterOnDraw());
        scr_center.addView(ll_center);
        scr_top.addView(getTopOnDraw());
        scr_down.addView(getBottomOnDraw());
        scr_left.addView(getLeftOnDraw());
    }

    private void setWH() {
        if (getTopOnDraw() == null) {
            touTextView.getLayoutParams().height = 0;
        } else {
            sz_ll.getLayoutParams().height = (int) (getTopOnDraw()
                    .getTotalHeight() * 1.5);
            touTextView.getLayoutParams().height = getTopOnDraw()
                    .getTotalHeight();
        }

        if (getLeftOnDraw() == null) {
            touTextView.getLayoutParams().width = 0;
        } else {
            sz_ll.getLayoutParams().width = getLeftOnDraw().getTotalWidth();
            touTextView.getLayoutParams().width = getLeftOnDraw()
                    .getTotalWidth();
        }

        if (getBottomOnDraw() == null) {
            diTextView.getLayoutParams().height = 0;
        } else {
            diTextView.getLayoutParams().height = getBottomOnDraw()
                    .getTotalHeight();
        }

        if (getLeftOnDraw() == null) {
            diTextView.getLayoutParams().width = 0;
        } else {
            diTextView.getLayoutParams().width = getLeftOnDraw()
                    .getTotalWidth();
        }
    }

    protected abstract ZouShiOnDraw getTopOnDraw();

    protected abstract ZouShiOnDraw getBottomOnDraw();

    protected abstract ZouShiOnDraw getLeftOnDraw();

    protected abstract ZouShiOnDraw getCenterOnDraw();

//    /**
//     * 添加布局，必须包含zoushi.xml
//     */
//    protected abstract void setContentView();

    protected abstract void change(int[][] kj);

    @Override
    public void onClick(View arg0) {
        Intent intent = null;
        switch (arg0.getId()) {
            case R.id.zst_shezhi_ll:
                intent = new Intent(getActivity(), ZST_sheZhi.class);

                intent.putExtra(ZST_sheZhi.ZST_QISHU, qiShu);
                intent.putExtra(ZST_sheZhi.ZST_ZHEXIAN, isZX);
                intent.putExtra(ZST_sheZhi.ZST_YILOU, isYL);
                intent.putExtra(ZST_sheZhi.ZST_TONGJI, isTJ);

                startActivityForResult(intent, 0);
                break;

            default:
                break;
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == 0 && null != data) {
            isZX = data.getBooleanExtra(ZST_sheZhi.ZST_ZHEXIAN, false);
            isYL = data.getBooleanExtra(ZST_sheZhi.ZST_YILOU, false);
            isTJ = data.getBooleanExtra(ZST_sheZhi.ZST_TONGJI, false);
            qiShu = data.getIntExtra(ZST_sheZhi.ZST_QISHU, 30);

            requestTrendChart();
        }
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean,
                             String result) {
        if (resultBean.getStatus() == 0) {
//            LogUtil.v(resultBean.getData());
//            String rr = (resultBean.getData()).replace("\\", "");
//            LogUtil.v(rr);
            arrayChartItems = (ArrayList<DLTTrendChart>) JSON.parseArray(
                    resultBean.getValue(), DLTTrendChart.class);
            initHistoryList();
        }

    }

    @Override
    protected void onFailure(int requestCode) {

    }

}
